package com.tju.resource.oauth.controller.tb;

import com.tju.resource.annotation.Log;
import com.tju.resource.business.tb_material.TbMaterialService;
import com.tju.resource.business.tb_sharper.TbSharperServiceImpl;
import com.tju.resource.business.tb_supplier.TbSupplierService;
import com.tju.resource.model.TbMaterial;
import com.tju.resource.model.TbSharper;
import com.tju.resource.model.TbSupplier;
import com.tju.resource.result.Result;
import com.tju.resource.result.ResultEnum;
import com.tju.resource.result.ResultUtil;
import com.tju.resource.result.SimpleXls;
import com.tju.resource.tool.enum_utils.FunctionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

@RestController
public class TbMaterialImportController {

    @Autowired
    TbMaterialService service;

    @Autowired
    TbSharperServiceImpl spService;

    @Autowired
    TbSupplierService suService;

    private static final String prefix = "matlimpo";

    public static final Map<String, String> keyToBean = new HashMap<>() {{
        put("模具编号", "sharperSerialNumber");
        put("物料编号", "matlNumber");
        put("物料名称", "matlName");
        put("供应商代码", "supCode");
        put("物料批次号", "matlPart");
        put("物料单重", "matlWeight");
        put("物料重量单位", "matlUnit");
        put("预热时间", "matlPrewarm");
        put("物料所在位置", "matlPosi");
        put("物料生产日期", "matlProd");
        put("物料来源", "matlSrc");
        put("物料有效日期", "matlDate");
        put("颜色", "color");
        put("额定工时", "workTime");
        put("水口添加比例", "wdPercent");
        put("水口重量", "wdWeight");
        put("穴数", "holeNum");
        put("材质", "material");
        put("半成品数量", "spPcs");
        put("成品数量", "fpPcs");
        put("数量单位", "pcsUnit");
        put("物料类型", "type");
        put("起斤量", "minWeight");
        put("上机费", "compensate");
        put("埋射配件", "buriedShot");
        put("镭雕内容编号", "radiumCarvingNumber");
        put("食品级", "foodGrade");
        put("喷涂颜色编号", "sprayingColorNumber");
        put("电镀", "electroplate");
        put("印刷内容编号", "printingNumber");
        put("单价", "unitVal");
    }};

    @PreAuthorize("hasAuthority('sys:spClient:supplier:import')")
    @PostMapping(prefix + "/import")
    @Log(type= FunctionType.Import)
    public Result<Object> importData(@RequestParam Map<String, String> params, @RequestParam("file") MultipartFile file) {
        try {
            return SimpleXls.saveAsBeans(
                    TbMaterial.class
                    , keyToBean
                    , file.getInputStream()
                    , service::add
                    , material -> {
                        TbSharper sharper = spService.checkSharperNameUnique(null, material.getSharperSerialNumber());
                        TbSupplier supplier = suService.byCode(material.getSupCode());

                        material.setCreateBy(params.get("createBy"));
                        material.setSid(null == sharper ? null : sharper.getId());
                        material.setSupCode(null == supplier ? null : supplier.getSupplierCode());
                    });
        } catch (Exception e) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, e.toString());
        }
    }

}
